package com.li.streamAPI;

import com.li.java2.Employee;
import com.li.java2.EmployeeData;
import org.junit.Test;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author 喂S别闹
 * @create 2022/4/21-11:13
 * @Version 1.0
 * @Description: 终止操作
 */
public class StreamAPITest3 {

    /**
     * @Date: 2022/4/21 11:14
     * @Auther: 喂S别闹
     * @param:
     * @Return: void
     * @Description:匹配与查找
     */
    @Test
    public void test1() {
        List<Employee> employees = EmployeeData.getEmployees();
//    allMatch(Predicate P) --检查是否匹配所有元素
//        练习：是否所有的员工年龄都大于18
        boolean b = employees.stream().allMatch(e -> e.getAge() > 18);
        System.out.println(b);

//        anyMatch(Predicate P) 检查是否至少匹配一个元素。练习：是否存在员工的工资大于10000
        boolean b1 = employees.stream().anyMatch(e -> e.getSalary() > 10000);
        System.out.println(b1);
//        noneMatch(Predicate P) 检查是否没有匹配的元素。    练习 是否存在姓 雷的
        boolean b3 = employees.stream().noneMatch(e -> e.getName().startsWith("雷"));
        System.out.println(b3);
//        findFirst  返回第一个元素
        Optional<Employee> first = employees.stream().findFirst();
        System.out.println(first);

        Optional<Employee> first1 = employees.parallelStream().findAny();
        System.out.println(first1);

//        count 返回总个数
        long count = employees.stream().count();
        System.out.println(count);

        long count1 = employees.stream().filter(e -> e.getSalary() > 5000).count();
        System.out.println(count1);
//        max(Comparator c)
//        返回最高工资
        Optional<Employee> max = employees.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(max);

        Stream<Double> doubleStream = employees.stream().map(e -> e.getSalary());
        Optional<Double> max1 = doubleStream.max(Double::compareTo);
        System.out.println(max1);

//        min
        //返回最低工资的员工
        Optional<Employee> min = employees.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(min);


    }

}
